package com.example.haohao.myview;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;

import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

public class MyText extends View {

    Paint paint;
    int result_width, result_height;
    private String mText;
    private int mColor;
    private int mSize;

    public MyText(Context context) {
        this(context, null);
    }

    public MyText(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyText(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);


        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyText);

        mText = array.getString(R.styleable.MyText_mText);
        mColor = array.getColor(R.styleable.MyText_mColor, Color.BLACK);
        mSize = array.getDimensionPixelSize(R.styleable.MyText_mSize, 15);

        array.recycle();
        paint = new Paint();
        paint.setTextSize(mSize);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        result_width=measureWidth(widthMeasureSpec);
        result_height=measureHeight(heightMeasureSpec);
        setMeasuredDimension(result_width, result_height);
    }

    private void measure2(int mode, int value) {

        int size = MeasureSpec.getSize(value);
        switch (mode) {

            case MeasureSpec.AT_MOST:     // wrap_content
                Rect rect = new Rect();
                paint.getTextBounds(mText, 0, mText.length(), rect);
                result_width = rect.width();
                result_height = rect.height();

                break;
            case MeasureSpec.EXACTLY:    // real value;  match_parent
//
//                result_height = size;
//                result_width = size;
                break;
            case MeasureSpec.UNSPECIFIED: // ??
//                result_width = 100;
//                result_height = 50;

                break;

        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        paint.setColor(mColor);

        canvas.drawText(mText,0,getHeight()/2, paint);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return super.onTouchEvent(event);
    }
    private int measureWidth(int measureSpec) {
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        //wrap_content
        if (specMode == MeasureSpec.AT_MOST){
            Rect rect = new Rect();
            paint.getTextBounds(mText, 0, mText.length(), rect);
            specSize = rect.width();
        }
        //fill_parent或者精确值
        else if (specMode == MeasureSpec.EXACTLY){

        }
        return specSize;
    }
    //根据xml的设定获取高度
    private int measureHeight(int measureSpec) {
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        //wrap_content
        if (specMode == MeasureSpec.AT_MOST){

            Rect rect = new Rect();
            paint.getTextBounds(mText, 0, mText.length(), rect);

            specSize = rect.height();
        }
        //fill_parent或者精确值
        else if (specMode == MeasureSpec.EXACTLY){


        }
        return specSize;
    }


}
